package com.tl.util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public class GenerateDataDemo {

	public static void main(String[] args) throws Exception {
//		for(int i=80;i<89;i++){
//			generate(i+"","2014");
//		}
		generate(88+"","2014");
	}
	
	//通过
	public static void generate(String issue,String year) throws SQLException{
		String sql = "SELECT hq1,hq2,hq3,hq4,hq5,hq6,lq1 FROM lottery WHERE batch=? and year!=?";
		Connection conn = JDBCUtils.getConn();
		PreparedStatement prep = conn.prepareStatement(sql);
		prep.setString(1, issue);
		prep.setString(2, year);
		ResultSet res = prep.executeQuery();
		ResultSetMetaData metaData = res.getMetaData();
		Map<String,Integer> hqMap = new HashMap<String, Integer>();
		Map<String,Integer> lqMap = new HashMap<String,Integer>();
		while(res.next()){
			for(int i=1;i<=metaData.getColumnCount();i++){
				String q = res.getString(i);
				if(i==metaData.getColumnCount()){
					Integer lqNum = lqMap.get(q);
					if(lqNum == null || lqNum == 0){
						lqMap.put(q, 1);
					}else{
						lqMap.put(q, lqNum+1);
					}
				}else{
					Integer lqNum = hqMap.get(q);
					if(lqNum == null || lqNum == 0){
						hqMap.put(q, 1);
					}else{
						hqMap.put(q, lqNum+1);
					}
				}
			}
			
		}
		JDBCUtils.closeConn(conn, prep, res);

		
		String[] hqArr = new String[hqMap.size()];
		String[] lqArr = new String[lqMap.size()];
		
		String hqStr = "红球：[";
		String lqStr = "篮球：[";
		
		int hqCount=0;
		int lqCount=0;

		List<Entry<String, Integer>> hqMapOrder = desOrder(hqMap);
		List<Entry<String, Integer>> lqMapOrder = desOrder(lqMap);
		
		
		for(Map.Entry<String, Integer> entry:hqMapOrder){
			String hqKey = entry.getKey();
			Integer hqVal = entry.getValue();
			hqStr += "["+hqKey+":"+hqVal+ "]";
			hqArr[hqCount]=hqKey;
			hqCount++;
		}
		
		for(Map.Entry<String, Integer> entry:lqMapOrder){
			String lqKey = entry.getKey();
			Integer lqVal = entry.getValue();
			lqStr += "["+lqKey+":"+lqVal+ "]";
			lqArr[lqCount]=lqKey;
			lqCount++;
		}
//		System.out.println(hqStr+"]");
//		System.out.println(lqStr+"]");
//		System.out.println();
	    
		
		/************************************************************************************************************************************************/
		
		//随机取球
		List<String> hqList = new ArrayList<String>();
		
		for(int qCount=1;qCount<7;qCount++){
			 if(hqList.size()>=6){break;}
			 Integer count = (int) (Math.random()*598%hqArr.length);
			 String tempHq = hqArr[count];
			 if(hqList.contains(tempHq)){
				 qCount--;
				 continue;
			 }
			 hqList.add(tempHq);
		}
		
		Integer lqNum = (int) (Math.random()*598%lqArr.length);
		String tempLq = lqArr[lqNum];
		
		String nhq = "随机取的红球：[";
		String nlq = "随机取的蓝球：[";
		for(String hqs : hqList){
			nhq += hqs+",";
		}
		nlq += tempLq ;
		System.out.println(nhq+"]");
		System.out.println(nlq+"]");
		
		
	    String sql2 = "SELECT hq1,hq2,hq3,hq4,hq5,hq6,lq1 FROM lottery WHERE batch=? and year=?";
	    Connection conn2 = JDBCUtils.getConn();
	    PreparedStatement prep2 = conn2.prepareStatement(sql2);
		prep2.setString(1, issue);
		prep2.setString(2, year);
		ResultSet res2 = prep2.executeQuery();
		ResultSetMetaData metaData2 = res2.getMetaData();
		List<String> list = new ArrayList<String>();
		String lqDB = "";
		while(res2.next()){
			for(int i=1;i<=metaData2.getColumnCount();i++){
				if(i!=metaData2.getColumnCount()){
					list.add(res2.getString(i));
				}else{
					lqDB = res2.getString(i);
				}
			}
		}
		
		
		
		String str = issue+"    [";
		String hqs = issue+"红球[";
		String lqs = issue+"篮球[";
		for(int j=0;j<list.size();j++){
			String s = list.get(j);
			str += s+",";
			if(hqMap.containsKey(s)){
				Integer count = hqMap.get(s);
				hqs +=  "["+s+":"+count+"]";
			}
		}
		if(lqMap.containsKey(lqDB)){
			Integer count = lqMap.get(lqDB);
			lqs +=  "["+lqDB+":"+count+"]";
		}
		
		System.out.println();
		System.out.println(str+ "  "+lqDB+ "]");
		System.out.println(hqs+"]");
		System.out.println(lqs+"]");
		
		System.out.println("-----------------------------------------------------");
		System.out.println();
		
	}
	
	
	
	
	
	
	
	
	
	
	//降序排序
	public static List<Map.Entry<String,Integer>> desOrder(Map<String,Integer> map){
		List<Map.Entry<String,Integer>> mappingList = new ArrayList<Map.Entry<String,Integer>>(map.entrySet());  //通过ArrayList构造函数把map.entrySet()转换成list 
	    
	    //通过比较器实现比较排序 
	    Collections.sort(mappingList, new Comparator<Map.Entry<String,Integer>>(){ 
		    public int compare(Map.Entry<String,Integer> o1,Map.Entry<String,Integer> o2){
		    	return o2.getValue().intValue() - o1.getValue().intValue();// 降序排列  
		    }
	    });
	    return mappingList;
	}
	
}
